package com.seednia.module.core.project.dto;

import org.apache.commons.lang.StringUtils;

import com.seednia.foundation.framework.dto.Constants;
import com.seednia.foundation.framework.dto.ICriteria;
import com.seednia.foundation.framework.dto.QueryParams;
import com.seednia.foundation.framework.util.NumberUtils;
import com.seednia.module.core.search.util.AbstractQueryBuilder;

public class ProjectQueryBuilder extends AbstractQueryBuilder {

	private String operatorStr = " OR";
	
	public ProjectQueryBuilder(QueryParams.Operator operator)
	{
		if (operator == QueryParams.Operator.AND) {
			operatorStr = " AND";
		}
	}
	
	@Override
	public String buildQueryString(ICriteria record) {
		ProjectCriteria criteria = (ProjectCriteria)record;

		StringBuilder retval = new StringBuilder();
		// OR-ed 
		/*
		if ( StringUtils.isNotBlank(dto.getCreatorDto().getFirstName()) ) {
			retval.append("firstName:(").append(dto.getCreatorDto().getFirstName()).append(")");
		}
		if ( StringUtils.isNotBlank(dto.getCreatorDto().getLastName()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" lastName:(").append(dto.getCreatorDto().getLastName()).append(")");
		}*/
		if ( StringUtils.isNotBlank(criteria.getName()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" name:(").append(AbstractQueryBuilder.escape(criteria.getName())).append(")");
		}
		if ( StringUtils.isNotBlank(criteria.getDescription()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" description:(").append(AbstractQueryBuilder.escape(criteria.getDescription())).append(")");
		}
		if ( StringUtils.isNotBlank(criteria.getGoal()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" goal:(").append(AbstractQueryBuilder.escape(criteria.getGoal())).append(")");
		}
		if ( StringUtils.isNotBlank(criteria.getChallenges()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" challenges:(").append(AbstractQueryBuilder.escape(criteria.getChallenges())).append(")");
		}
		
		// OR all the tags
		String tagNames[] = criteria.getRequiredSkillTagNames();
		if ( (tagNames != null) && (tagNames.length > 0 ) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			
			retval.append(" requiredSkillTagNamesCsv:(\"").append(AbstractQueryBuilder.escape(tagNames[0])).append("\"");
			for (int i=1; i < tagNames.length; i++) {
				retval.append(" OR ").append("\"").append(AbstractQueryBuilder.escape(tagNames[i])).append("\"");
			}
			retval.append(")");
		}
		
		// Enclose in parenthesis so we can AND them 
		if (retval.length() > 1) {
			retval.insert(0, "(").append(")");
		}
		
		if ( criteria.getLookingFor() > Constants.INT_ANY ) {
			// TODO: into or of bits
			Integer[] bitSetInts = NumberUtils.possibleBitSetIntegers(15, criteria.getLookingFor());
			if (bitSetInts != null && bitSetInts.length > 0) {
				if (retval.length() > 1) retval.append(" AND");
				retval.append( " lookingFor:(");
				for (int i=0; i < bitSetInts.length; i++) {
					if (i != 0)
						retval.append(" OR ");
					retval.append(bitSetInts[i]);
				}
				retval.append(")");
			}
		}
	
		// TODO: add author
		if ( criteria.getStatus() > Constants.INT_ANY ) {
			if (retval.length() > 1) retval.append(" AND");
			retval.append(" status:\"").append(criteria.getStatus()).append("\"");
		}
		if (retval.length() > 1) retval.append(" AND");
		retval.append(" privacyLevel:[0 TO ").append(criteria.getPrivacyLevel()).append("]");
				
		return retval.toString();
	}

}
